//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// The following people are credited with originating all or parts of this code:
// FlashDoctors | flashdoctors.sourceforge.net
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.net
{
	/**
	 * The query string class.
	 * 
	 * @author Tony Birleffi
	 */
	public class QueryString 
	{
		private var params:Object;
		private var keys:Array;
		
		/**
		 * Constructor.
		 * 
		 * @param qs	QueryString.
		 */
		public function QueryString(qs:String)
		{
			this.params = new Object();
			this.keys = new Array();
			if(qs != null)
			{
				if(qs.substr(0,1) == "?")
				{
					qs = qs.substr(1,qs.length);
				}
				var tmpArray:Array = qs.split("&");
				var tmpParam:Array;
				for(var i:Number = 0; i<tmpArray.length; i++)
				{
					tmpParam = String(tmpArray[i]).split("=");
					this.put(tmpParam[0],tmpParam[1]);
				}
			}
		}
		
		/**
		 * If it contains a key or not.
		 * 
		 * @param key	String.
		 * @return Boolean.
		 */
		public function containsKey(key:String):Boolean
		{
			var exists:Boolean = false;
			var n:Number;
			for(n=0; n<keys.length;n++)
			{
				if(String(this.keys[n]).toLowerCase() == key.toLowerCase())
				{
					exists = true;
				}
			}
			return exists;
		}
		
		/**
		 * Put into the querystring.
		 * 
		 * @param key	String.
		 * @param val	String.
		 * @return Boolean.
		 */
		public function put(key:String,val:String):Boolean
		{
			if(!containsKey(key))
			{
				this.keys.push(key);
				this.params[key] = val;
				return true;
			}
			return false;
		}
		
		/**
		 * Force put into the querystring.
		 * 
		 * @param key	String.
		 * @param val	String.
		 */
		public function forcePut(key:String, val:String):void 
		{
			if(!containsKey(key)) {
				put(key, val);
			}else {
				replace(key, val);
			}
		}
		
		/**
		 * Remove the key from the querystring.
		 * 
		 * @param key	String.
		 * @return Boolean.
		 */
		public function remove(key:String):Boolean
		{
			var i:Number;
			for(i=0; i<keys.length;i++)
			{
				if(String(this.keys[i]).toLowerCase() == key.toLowerCase())
				{
					this.keys.splice(i,1);
					delete this.params[key];
					return true;
				}
			}
			return false;	
		}
		
		/**
		 * Replace a key in the querystring.
		 * 
		 * @param key	String.
		 * @param val	String.
		 * @return Boolean.
		 */
		public function replace(key:String,val:String):Boolean
		{
			if(containsKey(key))
			{
				this.params[key] = val;
				return true;
			}
			return false;
		}
		
		/**
		 * Convert the querystring to just a string.
		 *
		 * @return String.
		 */
		public function toString():String
		{
			if(keys.length > 0) {
				var tmpString:String = "?";
				var i:Number;
				for(i=0; i<keys.length;i++)
				{
					tmpString = tmpString + this.keys[i] + "=" + this.params[this.keys[i]];
					if(i != this.keys.length -1)
					{
						tmpString = tmpString + "&";
					}
				}
				return tmpString;
			}
			return "";			
		}
		
		/**
		 * Get the keys array.
		 * 
		 * @return Array.
		 */
		public function getKeys():Array
		{
			return this.keys;	
		}
		
		/**
		 * Get the value.
		 * 
		 * @return String.
		 */
		public function getValue(key:String):String 
		{
			return params[key];
		}
	
	}
}
